package com.taiji.dianthus.controller.sys;


import com.alibaba.fastjson.JSONArray;
import com.taiji.dianthus.common.BasicController;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.domain.sys.DSysOrg;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.service.sys.OrgService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.util.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static com.taiji.dianthus.common.Constant.LOG_OPERATION_QUERY;

/**
 * @ClassName SysOrgViewController
 * @Description 组织相关
 * @Author H.M
 * @Date 2020/1/7
 */
@RequestMapping(value = "/sys/org")
@Controller
public class SysOrgViewController extends BasicController {

    @Autowired
    private OrgService orgService;

    @Autowired
    private SysLogService sysLogService;


    /**
     * -------------已弃用----------------------
     * 2020年6月24日 展现方法改为树状层级结构
     * 查询列表
     *
     * @param model
     * @param pageSize
     * @param currentPage
     * @return
     */
    @Deprecated
    @RequestMapping(value = "/list")
    public String list(Model model, Integer pageSize, Integer currentPage) {
        pageSize = getPageSize(pageSize);
        currentPage = getCurrentPage(currentPage);
        BusinessMessage message = orgService.list(currentPage, pageSize);
        model.addAttribute("data", message.getData());
        String url = "/sys/org/list?pagSize=10";
        model.addAttribute("url",url);
        model.addAttribute("currentUser",currentUser());
        model.addAttribute("menus",listMenu());
        return "/pages/sys/orgList";
    }

    /**
     * 组织管理界面
     * @param model
     * @return
     */
    @RequestMapping(value = "/treeList")
    public String treeList(HttpServletRequest request,Model model) {
        DSysUser currentUser = currentUser();
        JSONArray orgTree = orgService.getAllOrgTree();
        model.addAttribute("orgTree",orgTree);
        model.addAttribute("currentUser",currentUser );
        model.addAttribute("menus",listMenu());
        sysLogService.save(currentUser,LOG_OPERATION_QUERY,
                IPUtil.getIpAddr(request),"查询组织信息",1,"");
        return "/pages/sys/orgTreeList";
    }

    /**
     * * -------------已弃用----------------------
     * 2020年6月24日 展现方法改为树状层级结构
     * 新增页面
     *
     * @param model
     * @param id
     * @return
     */
    @Deprecated
    @RequestMapping(value = "/add")
    public String add(Model model, String id) {
        DSysOrg org = new DSysOrg();
        List<DSysOrg> parentOrgs = new ArrayList<>();
        if (null != id) {
            org = orgService.getOrgById(id);
            parentOrgs = orgService.listAllExclusionId(org.getId());
        }else {
            parentOrgs = orgService.listAll();
        }
        model.addAttribute("parentOrgs",parentOrgs);
        model.addAttribute("org", org);
        model.addAttribute("currentUser",currentUser());
        model.addAttribute("menus",listMenu());
        return "/pages/sys/orgAdd";
    }


    /**
     * 用户保存表单
     * @param model
     * @param dSysOrg
     * @return
     */
    @RequestMapping(value = "/save")
    public String save(Model model,DSysOrg dSysOrg){
        orgService.save(dSysOrg);
        return "redirect:/sys/org/list";
    }
}
